﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
namespace DevEx.Thanh.Class
{
    class CKiemKeTheoKhoController
    {
        private QLVTDataContext objDataContext;
        public CKiemKeTheoKhoController()
        {
            objDataContext = new QLVTDataContext();
        }
        public CKiemKeTheoKhoController(QLVTDataContext _obj)
        {
            objDataContext = _obj;
        }
        public void RefreshData()
        {
            objDataContext = new QLVTDataContext();
        }
        public tblKiemKe SelectRecordByID(int id)
        {
            try
            {
                return (from kk in objDataContext.tblKiemKes where kk.ID_KiemKe == id select kk).Single();
            }
            catch (Exception)
            {
                return null;
            }

        }
        public void Update(tblKiemKe obj)
        {
            objDataContext.tblKiemKes.Attach(obj);
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, obj);
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        public void UpdateWithoutAttach(tblKiemKe obj)
        {
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, obj);
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        public Boolean CheckIfExist(int dotKiemKe, int kho)
        {
            return (objDataContext.tblKiemKes.Any(kk => kk.DotKiemKe == dotKiemKe && (objDataContext.tblTons.Any(t=> t.ID_Kho ==kho && t.ID_MatHang==kk.ID_MatHang))));
        }
        public ISingleResult<sp_T_LayThongTinKiemKe_TheoKhoResult> LayThongTinKiemKe(int dotKiemKe, int kho)
        {
            try
            {
                return objDataContext.sp_T_LayThongTinKiemKe_TheoKho(dotKiemKe, kho);
            }
            catch (Exception)
            {
                return null;
            }
        }
        public void ThucHienKiemKe(int dotKiemKe, int kho)
        {
            if (dotKiemKe == 1)
            {
                objDataContext.sp_T_KiemKe11_TheoKho(kho);
            }
            else
            {
                objDataContext.sp_T_KiemKe17_TheoKho(kho);
            }
        }
    }
}
